package com.yanghui.lingyue.functions.ktBase

import com.icedata.sika.bridge.SikaEvent
import com.icedata.sika.bridge.SikaGroupEvent
import com.icedata.sika.bridge.SikaUserEvent
import com.icedata.sika.core.CommandProcessor
import com.icedata.sika.core.Sika
import com.yanghui.lingyue.bot.Bot
import com.yanghui.lingyue.data.Command
import com.yanghui.lingyue.functions.AbstractFunction
import com.yanghui.lingyue.utils.functionUtils.FunctionUtils
import com.yanghui.lingyue.utils.timeUtils.TimeUtils
import java.util.*

object SetBirthday : AbstractFunction() {
    override val name: String = "SetBirthday"

    override suspend fun parse(event: SikaEvent, sika: Sika): LinkedList<Command> {
        val command = LinkedList<Command>()
        if (event is SikaUserEvent) return command
        event as SikaGroupEvent
        if (!FunctionUtils.atBot(event)) return command
        if (!FunctionUtils.checkValid(event)) return command
        if (event.sendGroup.id !in enableGroup) return command
        val param = FunctionUtils.extractKeyword(event, "设置生日")
        param?.let {
            if (!TimeUtils.checkValidBirthday(param)) {
                command.add(Command(event, "TEXT: 格式错误，格式为YYYYMMDD", ""))
            } else {
                command.add(Command(event, name, it))
            }
        }
        return command
    }

    override suspend fun execute(command: Command, sika: Sika): CommandProcessor.Result {
        Bot.userList[command.sender.id]?.birthday = command.param
        Bot.sikaCore.databaseUpdate(Bot.userList[command.sender.id]!!)
        command.sendGroup?.sendMessage("设置生日 ${command.param}")
        return CommandProcessor.Result.ADOPT
    }
}